/**
 * 流程信息表 编辑页
 *
 * 作者 fenghaijun
 * 日期 2024-07-15 17:34:52
 */
layui.use(['jquery', 'febs', 'form', 'laydate', 'commonJS', 'element'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        commonJS = layui.commonJS,
        $view = $('#workflowInfo-detail'),
        validate = layui.validate,
        branchNode,
        workflowNodeId = POPUP_DATA.workflowNodeId;

    form.verify(validate);
    form.render();


    //流程设计
    let workflowData = new Map()
    var workflow = new Workflow({
        menus: [{
            type: 'approve', // 节点类型：node-条件节点
            icon: 'user-o', // 图标，参考 fontasome
            color: 'orange', // 图标颜色定制，默认蓝色
            text: '业务节点',
            node: { // 可预定义配置
                tips: '业务节点',
                icon: 'edit',
                name: '业务节点',
                text: '请配置业务节点', // 也支持function，this指向 properties
                properties: {}
            },
            // 自定义非空验证规则
            valid: function (data) {
                let approveNodeData = data.properties
                // 返回 false 验证通过
                return false
            },
            handler: function (node, event) {
                var el = node.el;
                var data = node.data;
                $.ux.drawer({
                    title: '',
                    html: $.ux.template('tpl-review', node.data),
                }).set('width', 640)
                //加载默认数据
                //自定义权限、事件下拉
                getBean(1, data.properties.workflowNodeCheckIds)
                // 找出页面除了抄送和条件的所有节点
                getAllNodes(data.nodeId)
                // 赋值初始化
                let $form = $(".ajs-dialog form")
                $("input[name=workflowNodeName]").val(data.name)
                $("input[name=workflowNodeId]").val(data.nodeId)
                $("input[name=workflowId]").val(workflowInfo.workflowId)

                if (!$.isEmptyObject(data.properties)) {
                    if (data.properties.workflowNodeCheckType == 5) {
                        $('.customType').show()
                        $('.approveUser').hide()
                    } else if (data.properties.workflowNodeCheckType == 2) {
                        $('.customType').hide()
                        $('.approveUser').hide()
                    } else {
                        $('.customType').hide()
                        $('.approveUser').show()
                    }
                    $("input[name=remark]").val(data.properties.remark)
                    $form.find("input[name='workflowNodeName']").val(data.properties.workflowNodeName)//节点名称
                    $form.find("#businessNodeId").val(data.properties.businessNodeId)//业务模块
                    $form.find("input[name='workflowNodeCheckType'][value='" + data.properties.workflowNodeCheckType + "']").attr("checked", true)//权限设置
                    $form.find("input[name='nodeBeforeReviewFlg'][value='" + data.properties.nodeBeforeReviewFlg + "']").attr("checked", true)//是否允许加签
                    $form.find("input[name='nodeModifyFlg'][value='" + data.properties.nodeModifyFlg + "']").attr("checked", true)//是否允许修改
                    $form.find("textarea[name='remark']").val(data.properties.remark)
                    // 权限设置-处理对象
                    let infoList = data.properties.workflowNodeCheckUserList
                    if (data.properties.workflowNodeCheckType == 1) {
                        //指定人员- 处理对象必选
                        let htl = ``;
                        infoList.forEach((item, index) => {
                            htl += `<span class="tag" data-id="${item.workflowNodeCheckIds}" data-name="${item.workflowNodeCheckIdsName}">${item.workflowNodeCheckIdsName}</span>`
                        })
                        $form.find('.selectName').html(htl)
                    }
                    if (data.properties.workflowNodeCheckType == 3) {
                        // 指定角色-处理对象必选
                        let htl = ``;
                        infoList.forEach((item, index) => {
                            htl += `<span class="tag" data-id="${item.workflowNodeCheckIds}" data-name="${item.workflowNodeCheckIdsName}">${item.workflowNodeCheckIdsName}</span>`
                        })
                        $form.find('.selectName').html(htl)
                    }
                    if (data.properties.workflowNodeCheckType == 4) {
                        // 指定部门-处理对象必选
                        let htl = ``;
                        infoList.forEach((item, index) => {
                            htl += `<span class="tag" data-id="${item.workflowNodeCheckIds}" data-name="${item.workflowNodeCheckIdsName}">${item.workflowNodeCheckIdsName}</span>`
                        })
                        $form.find('.selectName').html(htl)
                    }
                    if (data.properties.workflowNodeCheckType == 5) {
                        $form.find('#customQX').val(data.properties.workflowNodeCheckIds)
                    }
                    // 按钮设置赋值
                    // 获取按钮中按钮类型已经选中了哪些
                    let hasSelectedArr = []
                    if (data.properties.btnSetList && data.properties.btnSetList.length > 0) {
                        data.properties.btnSetList.forEach(item => {
                            hasSelectedArr.push(item.nodeButtonType)
                        })
                        let disabledA = false, disabledB = false, disabledC = false
                        if (hasSelectedArr.indexOf('1') != -1) {
                            disabledA = true
                        }
                        if (hasSelectedArr.indexOf('2') != -1) {
                            disabledB = true
                        }
                        if (hasSelectedArr.indexOf('3') != -1) {
                            disabledC = true
                        }
                        $('.allBtnContainer').empty()
                        let html = ''
                        data.properties.btnSetList.forEach((item, index) => {
                            html += `<dl class="buttonDiv">
                                <div class="first">
                                    <dt><span class="fc-star">*</span>按钮类型：</dt>
                                    <dd style="flex: auto;"  class="radioLine" data-oldvalue="${item.nodeButtonType}">
                                        <label class="fc-group">
                                            <input type="radio" name="nodeButtonType${index}" value="1" ${item.nodeButtonType == 1 ? 'checked' : ''} class="nodeTypeClick" disabled/>通过</label>
                                        <label class="fc-group">
                                            <input type="radio" name="nodeButtonType${index}" value="2" ${item.nodeButtonType == 2 ? 'checked' : ''} class="nodeTypeClick" disabled/>退回</label>
                                        <label class="fc-group">
                                            <input type="radio" name="nodeButtonType${index}" value="3" ${item.nodeButtonType == 3 ? 'checked' : ''} class="nodeTypeClick" disabled/>拒绝</label>
                                        <label class="fc-group">
                                            <input type="radio" name="nodeButtonType${index}" value="4" ${item.nodeButtonType == 4 ? 'checked' : ''} class="nodeTypeClick" disabled/>自定义</label>
                                    </dd>
                                </div>
                                <div>
                                    <dt><span class="fc-star">*</span>按钮id：</dt>
                                    <dd class="fc-form-inline">
                                        <input type="text" name="nodeButtonId" autocomplete="off" value="${item.nodeButtonId}" placeholder="请输入按钮id" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')" class="fc-ipt fc-ipt-auto btnidBlur" disabled>
                                    </dd>
                                </div>
                                <div class="second">
                                    <dt><span class="fc-star">*</span>按钮名称：</dt>
                                    <dd class="fc-form-inline">
                                        <input type="text" name="btnName" value="${item.nodeButtonName}" autocomplete="off" placeholder="请输入按钮名称" class="fc-ipt fc-ipt-auto" disabled>
                                    </dd>
                                </div>
                                <div class="third defaultRadio ${item.nodeButtonType == 4 ? 'fc-hide' : ''}">
                                    <dt><span class="fc-star">*</span>默认事件：</dt>
                                    <dd style="flex: auto;">
                                        <label class="fc-group">
                                            <input type="radio" name="defaultEvent${index}" value="0" ${item.defaultEvent == 0 ? 'checked' : ''} class="defaultEvent" disabled/>否</label>
                                        <label class="fc-group">
                                            <input type="radio" name="defaultEvent${index}" value="1" ${item.defaultEvent == 1 ? 'checked' : ''} class="defaultEvent" disabled/>是</label>
                                    </dd>
                                </div>
                                <div class="fourth ${item.defaultEvent == 0 || item.nodeButtonType == 4 ? '' : 'fc-hide'} liuzhuanBox">
                                    <dt><span class="fc-star">*</span>流转方式：</dt>
                                    <dd style="flex: auto;">
                                        <label class="fc-group">
                                            <input type="radio" name="transferWay${index}" value="1" ${item.transferWay == 1 ? 'checked' : ''} class="selectNodeRadio" disabled/>下一节点</label>
                                        <label class="fc-group">
                                            <input type="radio" name="transferWay${index}" value="2" ${item.transferWay == 2 ? 'checked' : ''} class="selectNodeRadio" disabled/>上一节点</label>
                                        <label class="fc-group">
                                            <input type="radio" name="transferWay${index}" value="3" ${item.transferWay == 3 ? 'checked' : ''} class="selectNodeRadio" disabled/>指定节点</label>
                                    </dd>
                                </div>`
                            if (item.transferWay == "3") {
                                html += `<div class="customNodeSelectLine">`
                            } else {
                                html += `<div class="customNodeSelectLine fc-hide">`
                            }
                            html += `<dt><span class="fc-star">*</span>指定节点：</dt>
                                    <dd style="flex: auto;">
                                        <select class="customNodeSelect" disabled>`
                            if (nodesAlls.length > 0) {
                                html += '<option value="">请选择</option>'
                                nodesAlls.forEach(x => {
                                    if (x.nodeId == item.transferNodeId) {
                                        html += `<option value="${x.nodeId}" selected>${x.nodeName}</option>`
                                    } else {
                                        html += `<option value="${x.nodeId}">${x.nodeName}</option>`
                                    }
                                })
                            } else {
                                html += '<option value="">暂无选项</option>'
                            }
                                        html+=`</select>
                                    </dd>
                                </div>
                                <div>
                                    <dt>自定义事件：</dt>
                                    <dd style="flex: auto;"><select class="customEvent" disabled>`
                            html += `<option value="">请选择</option>`
                            if (beanEventList && beanEventList.length > 0) {
                                beanEventList.forEach(subItem => {
                                    html += `<option value="${subItem}" ${item.customEvent == subItem ? 'selected' : ''}>${subItem}</option>`
                                })
                            } else {
                                html += `<option value="">暂无选项</option>`
                            }
                            html += `</select>
                                    </dd>
                                </div>
                            </dl>`
                        })
                        $('.allBtnContainer').append(html)
                    }
                }

                if ($.isEmptyObject(data.properties)) {
                    getBean(2, null)
                }
                // 查业务模块下拉
                getBussinessList(workflowInfo.workflowBusiness, data.properties)
                $(".ajs-button.ajs-ok").hide()
                $(".ajs-button.ajs-cancel").html("关闭")
                $(".ajs-button.ajs-cancel").css("background-color", "#1E9FFF");
                $(".ajs-button.ajs-cancel").css("color", "white");
            }
        },
            {
                type: 'notifier',
                icon: 'send',
                text: '抄送节点',
                color: 'green',
                node: {
                    tips: '抄送',
                    icon: 'send',
                    name: '',
                    color: 'green', // 定义不同色块(blue/green/orange)，也支持色值(#b99a01)
                    text: '请配置抄送节点', // 也支持function，this指向 properties
                },
                // 自定义非空验证规则
                valid: function (data) {
                    // 返回 false 验证通过
                    return false
                },
                handler: function (node, event) {
                    var el = node.el;
                    var data = node.data;
                    //初始化自定义权限
                    getNotifier()

                    $.ux.drawer({
                        title: '',
                        html: $.ux.template('tpl-copyFor', node.data),
                        onok: function (e) {
                            let $form = $(".ajs-dialog form")
                            if (e.button.className == "ajs-ok" && e.button.text == "确 定") {
                                let $form = $(".ajs-dialog form")
                                let workflowNodeName = $form.find("input[name='workflowNodeName']").val()//节点名称
                                let workflowNodeCheckType = $form.find("input[name='workflowNodeCheckType']:checked").val()//抄送类型
                                let remark = $form.find("textarea[name='remark']").val()//备注
                                let workflowInfoObj = {
                                    workflowNodeName: workflowNodeName,
                                    workflowNodeCheckType: workflowNodeCheckType,
                                    remark: remark
                                }
                                let workflowNodeReviewProperty = []
                                if (commonJS.isEmpty(workflowNodeName)) {
                                    layer.msg('请输入节点名称！', {
                                        icon: 5,
                                        zIndex: 1989202290,
                                        time: 1000
                                    })
                                    return false
                                }
                                if (commonJS.isEmpty(workflowNodeCheckType)) {
                                    layer.msg('请选择抄送类型！', {
                                        icon: 5,
                                        zIndex: 1989202290,
                                        time: 1000
                                    })
                                    return false
                                }
                                let workflowNodeCheckIds = null, workflowNodeCheckIdsName = ''
                                let tagList = $form.find(".selectName").find(".tag");
                                if (workflowNodeCheckType == 1) {
                                    //指定人员- 处理对象必选
                                    for (let i = 0; i < tagList.length; i++) {
                                        let id = $($(tagList)[i]).attr("data-id")
                                        let name = $($(tagList)[i]).attr("data-name")
                                        let object = {
                                            workflowNodeCheckType: workflowNodeCheckType,
                                            workflowNodeCheckIds: id,
                                            workflowNodeCheckIdsName: name
                                        }
                                        workflowNodeReviewProperty.push(object);
                                        workflowNodeCheckIds = id
                                        workflowNodeCheckIdsName = name
                                    }
                                }
                                if (workflowNodeCheckType == 3) {
                                    // 指定角色-处理对象必选
                                    // workflowNodeCheckIds = $form.find("input[name=roleId]").val()
                                    // workflowNodeCheckIdsName = $form.find("input[name=roleName]").val()
                                    for (let i = 0; i < tagList.length; i++) {
                                        let id = $($(tagList)[i]).attr("data-id")
                                        let name = $($(tagList)[i]).attr("data-name")
                                        let object = {
                                            workflowNodeCheckType: workflowNodeCheckType,
                                            workflowNodeCheckIds: id,
                                            workflowNodeCheckIdsName: name
                                        }
                                        workflowNodeReviewProperty.push(object);
                                        workflowNodeCheckIds = id
                                        workflowNodeCheckIdsName = name
                                    }
                                }
                                if (workflowNodeCheckType == 4) {
                                    // 指定部门-处理对象必选
                                    // workflowNodeCheckIds = $form.find("input[name=deptId]").val()
                                    // workflowNodeCheckIdsName = $form.find("input[name=deptName]").val()
                                    for (let i = 0; i < tagList.length; i++) {
                                        let id = $($(tagList)[i]).attr("data-id")
                                        let name = $($(tagList)[i]).attr("data-name")
                                        let object = {
                                            workflowNodeCheckType: workflowNodeCheckType,
                                            workflowNodeCheckIds: id,
                                            workflowNodeCheckIdsName: name
                                        }
                                        workflowNodeReviewProperty.push(object);
                                        workflowNodeCheckIds = id
                                        workflowNodeCheckIdsName = name
                                    }
                                }
                                if (workflowNodeCheckType == 5) {
                                    let object = {
                                        workflowNodeCheckType: workflowNodeCheckType,
                                        workflowNodeCheckIds: $form.find("#notifierQX option:checked").val(),
                                        workflowNodeCheckIdsName: ""
                                    }
                                    workflowNodeReviewProperty.push(object);
                                    workflowNodeCheckIds = $form.find("#notifierQX option:checked").val()
                                }
                                if (workflowNodeCheckType == 1 || workflowNodeCheckType == 3 || workflowNodeCheckType == 4) {
                                    if (commonJS.isEmpty(workflowNodeCheckIds)) {
                                        layer.msg('请选择处理对象！', {
                                            icon: 5,
                                            zIndex: 1989202290,
                                            time: 1000
                                        })
                                        return false
                                    }
                                } else if (workflowNodeCheckType == 2) {

                                } else {
                                    if (commonJS.isEmpty(workflowNodeCheckIds)) {
                                        layer.msg('请选择自定义权限！', {
                                            icon: 5,
                                            zIndex: 1989202290,
                                            time: 1000
                                        })
                                        return false
                                    }
                                }
                                workflowInfoObj.workflowNodeCheckIds = workflowNodeCheckIds
                                workflowInfoObj.workflowNodeCheckIdsName = workflowNodeCheckIdsName
                                workflowInfoObj.workflowNodeCheckUserList = workflowNodeReviewProperty
                                workflow.setProperties(node.data, workflowInfoObj)
                                node.data.name = workflowNodeName
                                node.data.text = remark
                                workflow.setName(node, workflowNodeName)
                                workflow.setText(el, remark)
                                workflow.valid(node)
                            }
                        }
                    }).set('width', 640)
                    //加载默认数据
                    let $form = $(".ajs-dialog form")
                    if (!$.isEmptyObject(data.properties)) {
                        if (data.properties.workflowNodeCheckType == 1 || data.properties.workflowNodeCheckType == 3 || data.properties.workflowNodeCheckType == 4) {
                            $('.notifierType').hide()
                            $('.notifieUser').show()
                        } else if (data.properties.workflowNodeCheckType == 2) {
                            $('.notifierType').hide()
                            $('.notifieUser').hide()
                        } else if (data.properties.workflowNodeCheckType == 5) {
                            $('.notifieUser').hide()
                            $('.notifierType').show()
                        }
                        $form.find("input[name='workflowNodeName']").val(data.properties.workflowNodeName)//节点名称
                        $form.find("input[name='workflowNodeCheckType'][value='" + data.properties.workflowNodeCheckType + "']").attr("checked", true)//抄送类型
                        $form.find("textarea[name='remark']").val(data.properties.remark)
                        // 权限设置-处理对象
                        let infoList = data.properties.workflowNodeCheckUserList
                        if (data.properties.workflowNodeCheckType == 1) {
                            //指定人员- 处理对象必选
                            //指定人员- 处理对象必选
                            let htl = ``;
                            infoList.forEach((item, index) => {
                                htl += `<span class="tag" data-id="${item.workflowNodeCheckIds}" data-name="${item.workflowNodeCheckIdsName}">${item.workflowNodeCheckIdsName}</span>`
                            })
                            $form.find('.selectName').html(htl)
                        }
                        if (data.properties.workflowNodeCheckType == 3) {
                            // 指定角色-处理对象必选
                            let htl = ``;
                            infoList.forEach((item, index) => {
                                htl += `<span class="tag" data-id="${item.workflowNodeCheckIds}" data-name="${item.workflowNodeCheckIdsName}">${item.workflowNodeCheckIdsName}</span>`
                            })
                            $form.find('.selectName').html(htl)
                        }
                        if (data.properties.workflowNodeCheckType == 4) {
                            // 指定部门-处理对象必选
                            let htl = ``;
                            infoList.forEach((item, index) => {
                                htl += `<span class="tag" data-id="${item.workflowNodeCheckIds}" data-name="${item.workflowNodeCheckIdsName}">${item.workflowNodeCheckIdsName}</span>`
                            })
                            $form.find('.selectName').html(htl)
                        }
                        if (data.properties.workflowNodeCheckType == 5) {
                            $form.find('#notifierQX').val(data.properties.workflowNodeCheckIds)
                            getNotifier(data.properties.workflowNodeCheckIds)
                        }
                    }
                    $(".ajs-button.ajs-ok").hide()
                    $(".ajs-button.ajs-cancel").html("关闭")
                    $(".ajs-button.ajs-cancel").css("background-color", "#1E9FFF");
                    $(".ajs-button.ajs-cancel").css("color", "white");
                }
            },
            {
                type: 'condition',
                icon: 'sitemap',
                text: '条件分支',
                handler: function (node, event) {
                    var el = node.el;
                    var data = node.data;
                    $.ux.drawer({
                        title: '',
                        html: $.ux.template('tpl-condition', node.data),
                        onshow: function () {
                            let $form = $(".ajs-dialog form")
                            $form.find(".condition-form-field").hide()
                        },
                        onok: function (e) {
                            let $form = $(".ajs-dialog form")
                            if (e.button.className == "ajs-ok" && e.button.text == "确 定") {
                                let workflowNodeName = $form.find("input[name='workflowNodeName']").val()
                                let remark = $form.find("textarea[name='remark']").val()//备注
                                if (commonJS.isEmpty(workflowNodeName)) {
                                    layer.msg('请输入节点名称！', {
                                        icon: 5,
                                        zIndex: 1989202290,
                                        time: 1000
                                    })
                                    return false
                                }
                                if (commonJS.isEmpty($form.find("input[name='workflowNodeTransOrder']").val())) {
                                    layer.msg('请输入优先级！', {
                                        icon: 5,
                                        zIndex: 1989202290,
                                        time: 1000
                                    })
                                    return false
                                }
                                if (commonJS.isEmpty($form.find("input[name=workflowNodeTransType]:checked").val())) {
                                    layer.msg('请选择条件关系！', {
                                        icon: 5,
                                        zIndex: 1989202290,
                                        time: 1000
                                    })
                                    return false
                                }
                                let workflowInfoObj = {
                                    workflowNodeName: workflowNodeName,
                                    workflowNodeTransOrder: $form.find("input[name='workflowNodeTransOrder']").val(),
                                    workflowNodeTransType: $form.find("input[name=workflowNodeTransType]:checked").val(),
                                    remark: remark
                                }
                                let workflowNodeTransConditionList = [];
                                let flgs = false;
                                $('.conditionDiv').each((v, h) => {
                                    let newObj = {
                                        workflowNodeTransOrder: $form.find("input[name='workflowNodeTransOrder']").val(),
                                        workflowNodeTransType: $form.find("input[name=workflowNodeTransType]:checked").val(),
                                    }
                                    let conditionType = $(h).find(".first").find("input[type='radio']:checked").val()
                                    let obj = {
                                        conditionType: conditionType
                                    }
                                    if (commonJS.isEmpty(conditionType)) {
                                        layer.msg('请选择条件类型！', {
                                            icon: 5,
                                            zIndex: 1989202290,
                                            time: 1000
                                        })
                                        flgs = true;
                                        return false
                                    }
                                    let tagList = $(h).find(".selectName").find(".tag");
                                    if (conditionType == "1") {
                                        let onlyList = [];
                                        let isNotNullId = "";
                                        for (let i = 0; i < tagList.length; i++) {
                                            let id = $($(tagList)[i]).attr("data-id")
                                            let name = $($(tagList)[i]).attr("data-name")
                                            let only = {
                                                conditionType: conditionType,
                                                conditionKey: id,
                                                conditionKeyName: name,
                                                conditionOperation: $(h).find(".third").find("input[type='radio']:checked").val()
                                            };
                                            onlyList.push(only)
                                            isNotNullId = id;
                                        }
                                        newObj.workflowNodeTransConditions = onlyList
                                        if (commonJS.isEmpty(isNotNullId)) {
                                            layer.msg('请选择条件筛选！', {
                                                icon: 5,
                                                zIndex: 1989202290,
                                                time: 1000
                                            })
                                            flgs = true;
                                            return false
                                        }
                                        if (commonJS.isEmpty($(h).find(".third").find("input[type='radio']:checked").val())) {
                                            layer.msg('请选择条件公式！', {
                                                icon: 5,
                                                zIndex: 1989202290,
                                                time: 1000
                                            })
                                            flgs = true;
                                            return false
                                        }
                                    } else if (conditionType == "2") {
                                        let onlyList = [];
                                        let isNotNullId = "";
                                        for (let i = 0; i < tagList.length; i++) {
                                            let id = $($(tagList)[i]).attr("data-id")
                                            let name = $($(tagList)[i]).attr("data-name")
                                            let only = {
                                                conditionType: conditionType,
                                                conditionKey: id,
                                                conditionKeyName: name,
                                                conditionOperation: $(h).find(".third").find("input[type='radio']:checked").val()
                                            };
                                            onlyList.push(only)
                                            isNotNullId = id;
                                        }
                                        newObj.workflowNodeTransConditions = onlyList
                                        if (commonJS.isEmpty(isNotNullId)) {
                                            layer.msg('请选择条件筛选！', {
                                                icon: 5,
                                                zIndex: 1989202290,
                                                time: 1000
                                            })
                                            flgs = true;
                                            return false
                                        }
                                        if (commonJS.isEmpty($(h).find(".third").find("input[type='radio']:checked").val())) {
                                            layer.msg('请选择条件公式！', {
                                                icon: 5,
                                                zIndex: 1989202290,
                                                time: 1000
                                            })
                                            flgs = true;
                                            return false
                                        }
                                    } else if (conditionType == "3") {
                                        if (commonJS.isEmpty($(h).find(".lastLevel option:selected").val())) {
                                            layer.msg('请选择操作按钮！', {
                                                icon: 5,
                                                zIndex: 1989202290,
                                                time: 1000
                                            })
                                            flgs = true;
                                            return false
                                        }
                                        let onlyList = [];
                                        let only = {
                                            conditionType: conditionType,
                                            conditionKey: $(h).find(".lastLevel option:selected").val(),
                                            conditionKeyName: "",
                                            conditionOperation: $(h).find(".third").find("input[type='radio']:checked").val()
                                        };
                                        onlyList.push(only)
                                        newObj.workflowNodeTransConditions = onlyList
                                    } else if (conditionType == "4") {
                                        if (commonJS.isEmpty($(h).find(".custom option:selected").val())) {
                                            layer.msg('请选择自定义！', {
                                                icon: 5,
                                                zIndex: 1989202290,
                                                time: 1000
                                            })
                                            flgs = true;
                                            return false
                                        }
                                        let onlyList = [];
                                        let only = {
                                            conditionType: conditionType,
                                            conditionKey: $(h).find(".custom option:selected").val(),
                                            conditionKeyName: "",
                                            conditionOperation: $(h).find(".third").find("input[type='radio']:checked").val()
                                        };
                                        onlyList.push(only)
                                        newObj.workflowNodeTransConditions = onlyList
                                    }
                                    workflowNodeTransConditionList.push(newObj)
                                })
                                if (!flgs) {
                                    workflowInfoObj.workflowNodeTransConditionList = workflowNodeTransConditionList
                                    workflow.setProperties(node.data, workflowInfoObj)
                                    node.data.name = workflowNodeName
                                    node.data.text = remark
                                    workflow.setName(node, workflowNodeName)
                                    workflow.setText(el, remark)
                                    workflow.valid(node)
                                } else {
                                    return false;
                                }
                            }
                        }
                    }).set('width', 640)
                    branchNode = data.nodeId
                    getLast(branchNode, null)
                    getBranch()
                    //回写值
                    let $form = $(".ajs-dialog form")
                    if (!$.isEmptyObject(data.properties)) {
                        $form.find("input[name='workflowNodeName']").val(data.properties.workflowNodeName)//节点名称
                        $form.find("input[name='workflowNodeTransOrder']").val(data.properties.workflowNodeTransOrder)//优先级
                        $form.find("input[name='workflowNodeTransType'][value='" + data.properties.workflowNodeTransType + "']").attr("checked", true)//条件关系
                        let remark = $form.find("textarea[name='remark']").val(data.properties.remark)//备注
                        if (null != data.properties.workflowNodeTransConditionList && data.properties.workflowNodeTransConditionList.length > 0) {
                            $("#conditionDivs").empty()
                            let htl = ``;
                            data.properties.workflowNodeTransConditionList.forEach((subItem, index) => {
                                let spanHtl = ``;
                                let conditionOperation = "";
                                let conditionType = "";
                                let conditionKey = "";
                                if (subItem.workflowNodeTransConditions && subItem.workflowNodeTransConditions.length > 0) {
                                    conditionKey = subItem.workflowNodeTransConditions[0].conditionKey
                                    conditionOperation = subItem.workflowNodeTransConditions[0].conditionOperation
                                    conditionType = subItem.workflowNodeTransConditions[0].conditionType
                                    subItem.workflowNodeTransConditions.forEach((items, index) => {
                                        spanHtl += `<span class="tag" data-id="${items.conditionKey}" data-name="${items.conditionKeyName}">${items.conditionKeyName}</span>`
                                    })
                                }
                                htl += `<dl class="conditionDiv">
                                           
                                                <div class="first">
                                                    <dt><span class="fc-star">*</span>条件类型：</dt>
                                                     <dd style="flex: auto;">
                                                         <label class="fc-group">
                                                            <input type="radio" name="conditionType${index}" value="1" data-type="4" ${conditionType == 1 ? 'checked' : ''} disabled/>
                                                            发起人部门
                                                         </label>
                                                        <label class="fc-group">
                                                            <input type="radio" name="conditionType${index}" value="2" data-type="3" ${conditionType == 2 ? 'checked' : ''}  disabled/>
                                                            发起人角色
                                                        </label>
                                                        <label class="fc-group">
                                                            <input type="radio" name="conditionType${index}" value="3" data-type="1" ${conditionType == 3 ? 'checked' : ''} disabled/>
                                                         上一级操作
                                                        </label>
                                                        <label class="fc-group">
                                                            <input type="radio" name="conditionType${index}" value="4" data-type="2" ${conditionType == 4 ? 'checked' : ''}  disabled/>
                                                        自定义
                                                         </label>
                                                     </dd>
                                                </div>`
                                htl += `<div class="second screen" style="${(conditionType == 1 || conditionType == 2) ? '' : 'display:none'}">
                                                    <dt><span class="fc-star">*</span>条件筛选：</dt>
                                                    <dd style="display: flex;align-items: center">
                                                        <div style="margin-top: 0;" class="selectName">${spanHtl}</div>
                                                    </dd>
                                                </div>
                                                <div class="third formual" style="${(conditionType == 1 || conditionType == 2) ? '' : 'display:none'}">
                                                    <dt><span class="fc-star">*</span>条件公式：</dt>
                                                    <dd style="flex: auto;">
                                                        <label class="fc-group">
                                                            <input type="radio" name="conditionOperation${index}" value="1" ${conditionOperation == 1 ? 'checked' : ''} disabled/>
                                                            属于
                                                        </label>
                                                        <label class="fc-group">
                                                            <input type="radio" name="conditionOperation${index}" value="2" ${conditionOperation == 2 ? 'checked' : ''} disabled/>
                                                            不属于
                                                        </label>
                                                        <label class="fc-group condition-form-field" style="display: none">
                                                            <input type="radio"  name="conditionOperation${index}" value="3" ${conditionOperation == 3 ? 'checked' : ''} disabled/>
                                                            大于
                                                           </label>
                                                        <label class="fc-group condition-form-field" style="display: none">
                                                            <input type="radio"  name="conditionOperation${index}" value="4" ${conditionOperation == 4 ? 'checked' : ''} disabled/>
                                                        大于等于
                                                        </label>
                                                        <label class="fc-group condition-form-field" style="display: none">
                                                            <input type="radio" name="conditionOperation${index}" value="5" ${conditionOperation == 5 ? 'checked' : ''} disabled/>
                                                        小于
                                                        </label>
                                                        <label class="fc-group condition-form-field" style="display: none">
                                                            <input type="radio" name="conditionOperation${index}" value="6" ${conditionOperation == 6 ? 'checked' : ''} disabled/>
                                                        小于等于
                                                        </label>
                                                    </dd>
                                                </div>`
                                htl += `<div class="lastLevel" style="${conditionType == 3 ? '' : 'display:none'}">
                                                    <dt><span class="fc-star">*</span>操作按钮：</dt>
                                                    <dd>
                                                        <select class="lastNode" disabled>`
                                htl += '<option value="">请选择</option>'
                                if (lastNodes && lastNodes.length > 0) {
                                    lastNodes.forEach(item => {
                                        htl += `<option value="${item.nodeButtonId}" ${item.nodeButtonId == conditionKey ? 'selected' : ''}>${item.nodeButtonName}</option>`
                                    })
                                }
                                htl += `</select>
                                                    </dd>
                                                </div>`
                                htl += `<div class="custom" style="${conditionType == 4 ? '' : 'display:none'}">
                                                    <dt><span class="fc-star">*</span>自定义：</dt>
                                                    <dd>
                                                        <select disabled>`
                                htl += '<option value="">请选择</option>'
                                if (branchList && branchList.length > 0) {
                                    branchList.forEach(item => {
                                        htl += `<option value="${item}" ${item == conditionKey ? 'selected' : ''}>${item}</option>`
                                    })
                                }
                                htl += `</select></dd></div>`
                                htl += `</dl>`;
                            })
                            $("#conditionDivs").append(htl)
                        }
                    }
                    $(".ajs-button.ajs-ok").hide()
                    $(".ajs-button.ajs-cancel").html("关闭")
                    $(".ajs-button.ajs-cancel").css("background-color", "#1E9FFF");
                    $(".ajs-button.ajs-cancel").css("color", "white");
                }
            }],
    })

    form.on('submit(workflowInfo-edit-form-submit)', function (data) {

        //流程设计信息
        let workflowDesignJson = null

        if (workflow.validAll()) {
            workflowDesignJson = workflow.getJson()
        } else {
            layer.msg("请检查各个节点是否全部配置完善", {icon: 3, time: 1000});
            return false
        }
        let workflowInfos = {
            workflowId: workflowInfo.workflowId,
            tenantId: workflowInfo.tenantId,
            workflowJson: workflowDesignJson
        }
        let workflowSaveRequest = {
            workflowInfo: workflowInfos
        }
        febs.postArray(ctx + 'workflowInfo/save', workflowSaveRequest, function (r) {
            if (r.code == "200") {
                layer.msg("保存成功", {icon: 1, time: 1000}, function () {
                    layer.closeAll();
                });
            } else {
                layer.msg("保存失败", {icon: 2, time: 1000});
            }
        });
        //addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {

    }

    var beanEventList = []

    function getBean(beanType, selectVal) {
        febs.get(ctx + 'workflowNode/queryWorkflowCustomBean', {classType: beanType}, function (res) {
            if (res.code === 200) {
                let html = '<option value="">请选择</option>'
                if (res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        html += `<option value="${item}">${item}</option>`
                    })
                } else {
                    html = `<option value="暂无选项">暂无选项</option>`
                }
                if (beanType == 1) {
                    $('#customQX').empty()
                    $('#customQX').append(html)
                    if (selectVal) {
                        $('#customQX').val(selectVal)
                    }
                } else {
                    $('.customEvent').empty()
                    $('.customEvent').append(html)
                    beanEventList = res.data
                }
            }
        });
    }

    var bussinessList = []

    function getBussinessList(workflowBusiness, properties) {
        febs.get(ctx + 'workflowInfo/getNodeByWorkflowName', {workflowBusiness: workflowBusiness}, function (res) {
            // 赋值业务模块下拉
            if (res.code == 200) {
                $('#businessNodeId').empty()
                if (res.data && res.data.length > 0) {
                    let html = '<option value="">请选择</option>'
                    res.data.forEach(item => {
                        html += `<option value="${item.nodeId}">${item.nodeName}</option>`
                    })
                    $('#businessNodeId').append(html)
                    bussinessList = res.data
                } else {
                    $('#businessNodeId').append('<option value="">暂无选项</option>')
                }
                if (!$.isEmptyObject(properties)) {
                    // 业务模块编辑回显
                    $('#businessNodeId').val(properties.businessNodeId)
                }
            }
        })
    }

    var nodesAll = []
    var nodesAlls = []
    // 获取所有节点除去抄送和条件,去掉点击的自己
    function getAllNodes(nodeId) {
        let objAll = JSON.parse(workflow.getJson())
        nodesAll = []
        fn(objAll)
        $('.customNodeSelect').empty()
        let optionHtml = '<option value="">请选择</option>'
        if (nodesAll.length > 0) {
            let otherNodesAll = nodesAll.filter(item => {
                if (item.nodeId != nodeId && !commonJS.isEmpty(nodeId)) {
                    return true
                } else {
                    return false
                }
            })
            nodesAlls=otherNodesAll
            if (otherNodesAll.length > 0) {
                otherNodesAll.forEach(item => {
                    optionHtml += `<option value="${item.nodeId}">${item.nodeName}</option>`
                })
            } else {
                optionHtml = '<option value="">暂无选项</option>'
            }

        }
        $('.customNodeSelect').append(optionHtml)
    }

    function fn(objAll) {
        if (objAll.type == "approve") {
            let obj = {
                nodeName: objAll.name || '',
                nodeId: objAll.nodeId
            }
            nodesAll.push(obj)
        }
        if (!!objAll.childNode) {
            fn(objAll.childNode)
        }
        if (objAll.conditionNodes && objAll.conditionNodes.length > 0) {
            objAll.conditionNodes.forEach(item => {
                if (!!item.childNode) {
                    fn(item.childNode)
                }
            })
        }
    }

    //抄送自定义权限
    function getNotifier(workflowNodeCheckIds) {
        febs.get(ctx + 'workflowNode/queryWorkflowCustomBean', {classType: "3"}, function (res) {
            if (res.code === 200) {
                let html = '<option value="">请选择</option>'
                if (res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        html += `<option value="${item}">${item}</option>`
                    })
                } else {
                    html = `<option value="暂无选项">暂无选项</option>`
                }
                $('#notifierQX').empty()
                $('#notifierQX').append(html)
                $('#notifierQX').val(workflowNodeCheckIds)
            }
        });
    }

    var lastNodes;
    var map = new Map();

    function getLast(nodeId, selectClass) {
        let objAll = JSON.parse(workflow.getJson())
        lastNodes = []
        fnLast(objAll, nodeId)
        if (selectClass) {
            $('.' + selectClass).empty()
        } else {
            $(".lastNode").empty()
        }
        let optionHtml = '<option value="">请选择</option>'
        if (lastNodes.length > 0) {
            for (let i = 0; i < lastNodes.length; i++) {
                optionHtml += `<option value="${lastNodes[i].nodeButtonId}">${lastNodes[i].nodeButtonName}</option>`
            }
            $(".lastNode").parents().find(".first").find("input[value='3']").removeAttr('disabled')
        } else {
            optionHtml = '<option value="">暂无选项</option>'
            $(".lastNode").parents().find(".first").find("input[value='3']").prop('disabled', true)
        }
        if (selectClass) {
            $('.' + selectClass).append(optionHtml)
        } else {
            $(".lastNode").append(optionHtml)
        }
    }

    //obj当前的数据  targetNodeId对应的那个节点
    function fnLast1(obj, targetNodeId) {
        if (obj) {
            map.set(obj.nodeId, obj)
            if (obj.type == "notifier") {//抄送节点
                if (obj.childNode) {
                    fnLast1(obj.childNode, targetNodeId, obj.prevId)
                }
            } else if (obj.type == "route") {//添加分支节点
                for (let i = 0; i < obj.conditionNodes.length; i++) {
                    let conditionNode = obj.conditionNodes[i];
                    if ("condition" == conditionNode.type) {//分支节点
                        if (conditionNode.nodeId == targetNodeId) {//判断是否与传进来的nodeId相等
                            findApproveNode(conditionNode.prevId)
                        } else {
                            map.set(conditionNode.nodeId, conditionNode)
                            fnLast1(conditionNode.childNode, targetNodeId, conditionNode.prevId)
                        }
                    } else {
                        map.set(conditionNode.nodeId, conditionNode)
                        fnLast1(conditionNode.childNode, targetNodeId, conditionNode.prevId)
                    }
                }
            } else if (obj.type == "approve") {
                if (obj.childNode) {
                    fnLast1(obj.childNode, targetNodeId, obj.prevId)
                }
            }
        }
    }

    function findApproveNode(nodeId) {
        let prevNode = map.get(nodeId);
        if (prevNode) {
            if (prevNode.nodeId && "approve" == prevNode.type) {
                let obj = map.get(prevNode.nodeId);
                lastNodes = obj.properties.btnSetList;
            } else {
                findApproveNode(prevNode.prevId)
            }
        }
    }

    function fnLast(objAll, nodeId) {
        var oneChild = objAll;
        fnLast1(oneChild.childNode, nodeId)
    }

    //分支自定义权限
    let branchList = []

    function getBranch(selectClass) {
        febs.get(ctx + 'workflowNode/queryWorkflowCustomBean', {classType: "2"}, function (res) {
            if (res.code === 200) {
                let html = '<option value="">请选择</option>'
                if (res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        html += `<option value="${item}">${item}</option>`
                    })
                } else {
                    html = `<option value="暂无选项">暂无选项</option>`
                }
                if (selectClass) {
                    $('.' + selectClass).empty()
                    $('.' + selectClass).append(html)
                } else {
                    $('.branchSelect').empty()
                    $('.branchSelect').append(html)
                }
                branchList = res.data
            }
        });
    }

    function lightLabel(objAll, workflowNodeUuid) {
        var oneChild = objAll;
        fnLabel(oneChild.childNode, workflowNodeUuid)
    }

    function fnLabel(obj, workflowNodeUuid) {
        if (obj) {
            if (obj.type == "notifier") {//抄送节点
                if (obj.childNode) {
                    fnLabel(obj.childNode, workflowNodeUuid)
                }
            } else if (obj.type == "route") {//添加分支节点
                for (let i = 0; i < obj.conditionNodes.length; i++) {
                    let conditionNode = obj.conditionNodes[i];
                    fnLabel(conditionNode.childNode, workflowNodeUuid)
                }
            } else if (obj.type == "approve") {
                if (obj.uuid == workflowNodeUuid) {
                    // $(".fc-tree-wrap.fc-node-"+obj.nodeId+"").find(".fc-node-label").css("box-shadow","rgb(255 222 78) 0px 0px 4px 3px")
                    // $(".fc-tree-wrap.fc-node-"+obj.nodeId+"").find(".fc-tree-actor").css("transform","scale(1.1)")
                    $(".fc-tree-wrap.fc-node-" + obj.nodeId + "").find(".fc-tree-actor").css("width", "230px")
                    $(".fc-tree-wrap.fc-node-" + obj.nodeId + "").find(".fc-tree-actor").css("height", "100px")
                    $(".fc-tree-wrap.fc-node-" + obj.nodeId + "").find(".fc-node-label").css("margin-top", "5px")
                    $(".fc-tree-wrap.fc-node-" + obj.nodeId + "").find(".fc-tree-actor").addClass("box")
                    $(".fc-tree-wrap.fc-node-" + obj.nodeId + "").find(".fc-tree-actor").addClass("horseRun")


                } else {
                    if (obj.childNode) {
                        fnLabel(obj.childNode, workflowNodeUuid)
                    }
                }
            }
        }
    }

    // 流程回显
    if (workflowInfo && workflowInfo.workflowJson) {
        let workflowDesignJson = JSON.parse(workflowInfo.workflowJson)
        if (workflowDesignJson.hasOwnProperty("childNode")) {
            workflow.load(JSON.parse(workflowInfo.workflowJson))
        }
        $view.find("button.fc-btn").css("pointer-events", "none").hide()
        $view.find("#reviewFlow  button").css("pointer-events", "none").hide()
        $view.find("#reviewFlow  button.add-col-btn").html("分支条件")
        if (null != workflowNodeId && workflowNodeId != '') {
            let data = {
                workflowNodeId: workflowNodeId
            }
            febs.postArray(ctx + 'workflowNode/selectInfoByWorkflowNodeId', data, function (data) {
                if (data.code == "200") {
                    lightLabel(workflowDesignJson, data.data.workflowNodeUuid)
                }
            });
        }
        getBean(1, null);
        getBean(2, null);
    }
});